using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using dender.dvdcover.core;
using System.Drawing.Printing;
using dender.dvdcover.gui.Properties;
using System.IO;
using System.Diagnostics;

namespace dender.dvdcover.gui
{
    public partial class coverForm : Form
    {
        private FormatInventory mFormatInventory;

        public coverForm()
        {
            InitializeComponent();
        }

        private void coverForm_Load(object sender, EventArgs e)
        {
            FillInventory();
        }

        public List<Format> Formats
        {
            get
            {
                return mFormatInventory;
            }
        }

        /// <summary>
        /// Tries to load the format xml file where al the formats are stored
        /// If the file not exists, it can be created (yes/no)
        /// If the file is corrupt it can be created (yes/no)
        /// </summary>
        /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception>
        /// <exception cref="System.IO.IOException">The target file is open or memory-mapped on a computer running Microsoft Windows NT. </exception>
        /// <exception cref="System.UnauthorizedAccessException">The path is a directory.</exception>
        private void FillInventory()
        {
            string fullname = Application.StartupPath + "\\" + Settings.Default.FormatInventoryFilename;
            mFormatInventory = new FormatInventory(fullname);
            RefreshListbox();
        }

        /// <summary>
        /// Reload the datasource
        /// </summary>
        private void RefreshListbox()
        {
            formatListbox.DataSource = null;
            formatListbox.DataSource = mFormatInventory;
            formatListbox.DisplayMember = "Description";
            formatListbox.Refresh();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (formatListbox.SelectedItem != null)
            {
                mainForm parent = (mainForm)this.Owner;
                Format f = (Format)formatListbox.SelectedItem;
                Cover c = new Cover(f);
                parent.Cover = c;
                this.Hide();
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (formatListbox.SelectedItem != null)
            {
                if (MessageBox.Show(Constants.STR_DELETE_FORMAT, "Delete Format", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    mFormatInventory.Remove((Format)formatListbox.SelectedItem);
                    mFormatInventory.Save();
                    RefreshListbox();
                }
            }
        }

        internal void AddFormat(Format f)
        {
            this.mFormatInventory.Add(f);
            RefreshListbox();
            mFormatInventory.Save();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not Implemented");
        }

        private void formatListbox_DoubleClick(object sender, EventArgs e)
        {
            // Quick and dirty here
            okButton_Click(null, null);
        }
    }
}